package GUI_3GE;

import exeptions.ControlledUserException;
import java.awt.*;
import java.util.ArrayList;
import pkg3hge_plh24.*;
import java.sql.SQLException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;


public class SearchRestaurantFrame extends javax.swing.JFrame {
 private DBManager dbm;
 private Users controlledUser;
 private ArrayList<Area> areas ;
 private ArrayList<Category> categ;
 private ArrayList<PriceRange> pr;
 private ArrayList<Restaurant> rests;
    
    public SearchRestaurantFrame (DBManager dbm, Users controlledUser) throws ControlledUserException {
        initComponents();

        this.dbm = dbm;
        this.controlledUser = controlledUser;

              

        try{
            areas = dbm.getArea();
            categ =dbm.getCategory();
            pr =dbm.getPriceRange();
            rests=dbm.getRestaurant();
             ComboBoxLists();
            }
       catch (SQLException ex) {
            Logger.getLogger(LogInFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ControlledUserException ex) {
            Logger.getLogger(LogInFrame.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    

// 
     private void ComboBoxLists() {

        // Areas  ComboBox
        DefaultComboBoxModel c1 = (DefaultComboBoxModel) restaurantAreaComboBox.getModel();

        for (int i = 0; i < areas.size(); i++) {
             Area ar = areas.get(i);
             c1.addElement(ar.getAreaName());
        }
        restaurantAreaComboBox.setModel(c1);
        
       //PriceRangeComboBox  
     DefaultComboBoxModel c2 = (DefaultComboBoxModel) restaurantAreaComboBox.getModel();
       for (int i = 0; i < pr.size(); i++) {
             PriceRange p = pr.get(i);
             c2.addElement(p.getPRName());
         }
        restaurantPriceRangeComboBox.setModel(c2);
        
        //restaurantCategoryList
                
        DefaultListModel l1 = new DefaultListModel();
        for (int i = 0; i < categ.size(); i++) {
             Category ca = categ.get(i);
             l1.addElement(ca.getCatName());
           }
        restaurantCategoryList.setModel(l1);
        
    }
     
     

   
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        userLogoLabel = new javax.swing.JLabel();
        userSearchRestaurantButton = new javax.swing.JButton();
        userStartServiceButton = new javax.swing.JButton();
        userStopServiceButton = new javax.swing.JButton();
        userExitButton = new javax.swing.JButton();
        searchRestaurantTextLabel = new javax.swing.JLabel();
        criteriaSearchPanel = new javax.swing.JPanel();
        restaurantNameLabel = new javax.swing.JLabel();
        restaurantNameTextField = new javax.swing.JTextField();
        restaurantAreaLabel = new javax.swing.JLabel();
        restaurantAreaComboBox = new javax.swing.JComboBox();
        restaurantCategoryLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        restaurantCategoryList = new javax.swing.JList();
        restaurantPriceRangeLabel = new javax.swing.JLabel();
        restaurantPriceRangeComboBox = new javax.swing.JComboBox();
        restaurantSearchButton = new javax.swing.JButton();
        clearCriteriaButton = new javax.swing.JButton();
        searchResultsPanel = new javax.swing.JPanel();
        searchResultsTextLabel = new javax.swing.JLabel();
        showRestaurantButton = new javax.swing.JButton();
        rateRestaurantButton = new javax.swing.JButton();
        bookRestaurantButton3 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Εφαρμογή City Guide");

        userLogoLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI_3GE/logo.png"))); // NOI18N
        userLogoLabel.setToolTipText("");
        userLogoLabel.setMaximumSize(new java.awt.Dimension(1145, 395));

        userSearchRestaurantButton.setText("Αναζήτηση Εστιατορίου");
        userSearchRestaurantButton.setEnabled(false);

        userStartServiceButton.setText("Εκκίνηση Υπηρεσίας");

        userStopServiceButton.setText("Διακοπή Υπηρεσίας");

        userExitButton.setText("Έξοδος");
        userExitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                userExitButtonActionPerformed(evt);
            }
        });

        searchRestaurantTextLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        searchRestaurantTextLabel.setText("Αναζήτηση Εστιατορίου");

        criteriaSearchPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Επιλέξτε Κριτήρια"));

        restaurantNameLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        restaurantNameLabel.setText("Όνομα Εστιατορίου:");
        restaurantNameLabel.setToolTipText("");

        restaurantNameTextField.setToolTipText("");
        restaurantNameTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                restaurantNameTextFieldActionPerformed(evt);
            }
        });

        restaurantAreaLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        restaurantAreaLabel.setText("Περιοχή:");
        restaurantAreaLabel.setToolTipText("");

        restaurantAreaComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                restaurantAreaComboBoxActionPerformed(evt);
            }
        });

        restaurantCategoryLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        restaurantCategoryLabel.setText("Κατηγορία:");
        restaurantCategoryLabel.setToolTipText("");

        restaurantCategoryList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Κατηγορία 1", "Κατηγορία 2", "Κατηγορία 3", " " };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(restaurantCategoryList);

        restaurantPriceRangeLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        restaurantPriceRangeLabel.setText("Εύρος Τιμών:");
        restaurantPriceRangeLabel.setToolTipText("");

        restaurantPriceRangeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                restaurantPriceRangeComboBoxActionPerformed(evt);
            }
        });

        restaurantSearchButton.setText("Αναζήτηση");
        restaurantSearchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                restaurantSearchButtonActionPerformed(evt);
            }
        });

        clearCriteriaButton.setText("Καθαρισμός Φίλτρων");
        clearCriteriaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearCriteriaButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout criteriaSearchPanelLayout = new javax.swing.GroupLayout(criteriaSearchPanel);
        criteriaSearchPanel.setLayout(criteriaSearchPanelLayout);
        criteriaSearchPanelLayout.setHorizontalGroup(
            criteriaSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(criteriaSearchPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(criteriaSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, criteriaSearchPanelLayout.createSequentialGroup()
                        .addGroup(criteriaSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(restaurantNameLabel)
                            .addComponent(restaurantAreaLabel)
                            .addComponent(restaurantCategoryLabel))
                        .addGap(18, 18, 18))
                    .addComponent(restaurantPriceRangeLabel, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGroup(criteriaSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
                    .addComponent(restaurantNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
                    .addComponent(restaurantPriceRangeComboBox, 0, 234, Short.MAX_VALUE)
                    .addGroup(criteriaSearchPanelLayout.createSequentialGroup()
                        .addComponent(restaurantSearchButton)
                        .addGap(18, 18, 18)
                        .addComponent(clearCriteriaButton))
                    .addComponent(restaurantAreaComboBox, 0, 234, Short.MAX_VALUE))
                .addGap(47, 47, 47))
        );
        criteriaSearchPanelLayout.setVerticalGroup(
            criteriaSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(criteriaSearchPanelLayout.createSequentialGroup()
                .addGroup(criteriaSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(restaurantNameLabel)
                    .addComponent(restaurantNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(criteriaSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(restaurantAreaLabel)
                    .addComponent(restaurantAreaComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(criteriaSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(restaurantCategoryLabel)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addGroup(criteriaSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(restaurantPriceRangeLabel)
                    .addComponent(restaurantPriceRangeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(criteriaSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(restaurantSearchButton)
                    .addComponent(clearCriteriaButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        restaurantAreaComboBox.getAccessibleContext().setAccessibleParent(restaurantAreaComboBox);

        searchResultsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        searchResultsTextLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        searchResultsTextLabel.setText("Αποτελέσματα Αναζήτησης");

        showRestaurantButton.setText("Προβολή");

        rateRestaurantButton.setText("Αξιολόγηση");

        bookRestaurantButton3.setText("Κράτηση");
        bookRestaurantButton3.setToolTipText("");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable1);

        javax.swing.GroupLayout searchResultsPanelLayout = new javax.swing.GroupLayout(searchResultsPanel);
        searchResultsPanel.setLayout(searchResultsPanelLayout);
        searchResultsPanelLayout.setHorizontalGroup(
            searchResultsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(searchResultsPanelLayout.createSequentialGroup()
                .addGroup(searchResultsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(searchResultsPanelLayout.createSequentialGroup()
                        .addGap(238, 238, 238)
                        .addComponent(showRestaurantButton, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rateRestaurantButton, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(bookRestaurantButton3))
                    .addGroup(searchResultsPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(searchResultsTextLabel))
                    .addGroup(searchResultsPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 703, Short.MAX_VALUE)))
                .addContainerGap())
        );
        searchResultsPanelLayout.setVerticalGroup(
            searchResultsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(searchResultsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(searchResultsTextLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addGroup(searchResultsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(showRestaurantButton)
                    .addComponent(rateRestaurantButton)
                    .addComponent(bookRestaurantButton3))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(searchResultsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(userLogoLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(searchRestaurantTextLabel)
                        .addGap(423, 423, 423))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(userExitButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(userStopServiceButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(userStartServiceButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(userSearchRestaurantButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addComponent(criteriaSearchPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(134, 134, 134))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(userLogoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(searchRestaurantTextLabel))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(userSearchRestaurantButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(userStartServiceButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(userStopServiceButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(userExitButton))
                    .addComponent(criteriaSearchPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(searchResultsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(40, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void userExitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_userExitButtonActionPerformed
        System.exit(0); //έξοδος}//GEN-LAST:event_userExitButtonActionPerformed
    }
private void restaurantSearchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_restaurantSearchButtonActionPerformed
// TODO add your handling code here:
                                              

String name = restaurantNameTextField.getText();
  
        if (name.equals(""))
            name = null;
        
      
         Area ar = null;
        if (restaurantAreaComboBox.getSelectedIndex() > 0)
            ar = areas.get(restaurantAreaComboBox.getSelectedIndex()-1);
        
          Category cat = null;
        if (restaurantCategoryList.getSelectedIndex() != -1)
            cat = categ.get(restaurantCategoryList.getSelectedIndex());

        PriceRange pra = null;
        if (restaurantPriceRangeComboBox.getSelectedIndex() > 0)
            pra = pr.get(restaurantPriceRangeComboBox.getSelectedIndex());

        


}//GEN-LAST:event_restaurantSearchButtonActionPerformed

private void restaurantPriceRangeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_restaurantPriceRangeComboBoxActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_restaurantPriceRangeComboBoxActionPerformed

private void restaurantNameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_restaurantNameTextFieldActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_restaurantNameTextFieldActionPerformed

    private void clearCriteriaButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearCriteriaButtonActionPerformed
        // καθαρισμός πεδίων φόρμας
        
        
    }//GEN-LAST:event_clearCriteriaButtonActionPerformed

private void restaurantAreaComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_restaurantAreaComboBoxActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_restaurantAreaComboBoxActionPerformed

    
   
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bookRestaurantButton3;
    private javax.swing.JButton clearCriteriaButton;
    private javax.swing.JPanel criteriaSearchPanel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton rateRestaurantButton;
    private javax.swing.JComboBox restaurantAreaComboBox;
    private javax.swing.JLabel restaurantAreaLabel;
    private javax.swing.JLabel restaurantCategoryLabel;
    private javax.swing.JList restaurantCategoryList;
    private javax.swing.JLabel restaurantNameLabel;
    private javax.swing.JTextField restaurantNameTextField;
    private javax.swing.JComboBox restaurantPriceRangeComboBox;
    private javax.swing.JLabel restaurantPriceRangeLabel;
    private javax.swing.JButton restaurantSearchButton;
    private javax.swing.JLabel searchRestaurantTextLabel;
    private javax.swing.JPanel searchResultsPanel;
    private javax.swing.JLabel searchResultsTextLabel;
    private javax.swing.JButton showRestaurantButton;
    private javax.swing.JButton userExitButton;
    private javax.swing.JLabel userLogoLabel;
    private javax.swing.JButton userSearchRestaurantButton;
    private javax.swing.JButton userStartServiceButton;
    private javax.swing.JButton userStopServiceButton;
    // End of variables declaration//GEN-END:variables
}
